﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Timers;
using DLL.Entities;

namespace MIS
{
    public partial class Form1 : Form
    {
        Missile[] Missiles = new Missile[5];
        WriteXML xWrite = new WriteXML(); 
        ReadXML xRead = new ReadXML(); 
        MIS_Config configM = new MIS_Config();
        int counter;

        public Form1()
        {
            InitializeComponent();

            tWrite.Enabled = true;
  
            //Attempts to Deserialize the xml file
            configM = xRead.ReadMISConfig();
            txtMisVersion.Text = configM.Version;
            txtMisModel.Text = configM.Model;
          
            Missiles = xRead.ReadMissile("../../../DLL/XML/Missile.xml");


            txtRemMis.Text = Convert.ToString(Missiles[0].MissileRemaining);
            txtMisName.Text = Missiles[0].MissileName;
            txtMisRange.Text = Convert.ToString(Missiles[0].MissileRange);
            txtMisMaxSpeed.Text = Convert.ToString(Missiles[0].MissileSpeed);
            txtMisWeight.Text = Convert.ToString(Missiles[0].MissileWeight);
            txtMisLength.Text = Convert.ToString(Missiles[0].MissileLength);
            txtMisDiam.Text = Convert.ToString(Missiles[0].MissileDiameter);
            txtMisType.Text = Missiles[0].MissileType;
            txtMisMaxAlt.Text = Convert.ToString(Missiles[0].MissileMaxAltitude);

            for (int i = 0; i < 5; i++)
            {
                cmboxMissiles.Items.Add(Missiles[i].MissileName);
            }

            cmboxMissiles.SelectedIndex = 0;

            txtMisModel.Text = Convert.ToString(configM.Model);
            txtMisVersion.Text = Convert.ToString(configM.Version);

            
            xWrite.WriteMISStatus("Online", Missiles[cmboxMissiles.SelectedIndex].MissileRemaining, Missiles[cmboxMissiles.SelectedIndex].MissileType, "../../../DLL/XML/Mis_Status.xml");

        }           

        private void cmboxMissiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtMisName.Text = Missiles[cmboxMissiles.SelectedIndex].MissileName;
            txtRemMis.Text = Convert.ToString(Missiles[cmboxMissiles.SelectedIndex].MissileRemaining);
            txtMisRange.Text = Convert.ToString(Missiles[cmboxMissiles.SelectedIndex].MissileRange);
            txtMisMaxSpeed.Text = Convert.ToString(Missiles[cmboxMissiles.SelectedIndex].MissileSpeed);
            txtMisWeight.Text = Convert.ToString(Missiles[cmboxMissiles.SelectedIndex].MissileWeight);
            txtMisLength.Text = Convert.ToString(Missiles[cmboxMissiles.SelectedIndex].MissileLength);
            txtMisDiam.Text = Convert.ToString(Missiles[cmboxMissiles.SelectedIndex].MissileDiameter);
            txtMisType.Text = Missiles[cmboxMissiles.SelectedIndex].MissileType;
            txtMisMaxAlt.Text = Convert.ToString(Missiles[cmboxMissiles.SelectedIndex].MissileMaxAltitude);
        }

        // Sends Offline status to COM when form is closed
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            tWrite.Enabled = false;
            xWrite.WriteMISStatus("Offline", 0, "N/A", "../../../DLL/XML/Mis_Status.xml");
        }

        private void btnFire_Click(object sender, EventArgs e)
        {
            Missiles[cmboxMissiles.SelectedIndex].MissileRemaining -= 1;
            txtRemMis.Text = Convert.ToString(Missiles[cmboxMissiles.SelectedIndex].MissileRemaining);
            impact.Enabled = true;
            countdown.Enabled = true;
            counter = 10;
            txtcountdown.Text = "10";
            MessageBox.Show("Missile launched!  Ten seconds to impact: to abort missile click Destruct.");


        }

        private void btnDestruct_Click(object sender, EventArgs e)
        {
            impact.Enabled = false;
            countdown.Enabled = false;
            txtcountdown.Text = "";
            MessageBox.Show("Missile Aborted!");

        }  

        private void impact_Tick(object sender, EventArgs e)
        {
            impact.Enabled = false;
            countdown.Enabled = false;
            MessageBox.Show("Target Destroyed!");

            xWrite.ClearTarget(0);
                      
        }

        private void countdown_Tick(object sender, EventArgs e)
        {
            txtcountdown.Text = Convert.ToString(--counter);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            tWrite.Enabled = true;
        }

        private void tWrite_Tick_1(object sender, EventArgs e)
        {
            xWrite.WriteMISStatus("Online", Missiles[cmboxMissiles.SelectedIndex].MissileRemaining, Missiles[cmboxMissiles.SelectedIndex].MissileType, "../../../DLL/XML/MIS_Status.xml");
        }

        

    }
}
